package cn.chenhao.org.service.impl;

import cn.chenhao.basic.service.impl.BasicServiceImpl;
import cn.chenhao.org.domain.Department;
import cn.chenhao.org.mapper.DepartmentMapper;
import cn.chenhao.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 添加功能
     * @param dept
     */
    @Override
    @Transactional
    public void add(Department dept) {
        departmentMapper.save(dept);
        this.update(dept);
    }

    /**
     * 修改
     * @param dept
     */
    @Override
    @Transactional
    public void update(Department dept) {
        // 封装dirPath列
        String dirPath = "";
        if (dept.getParent() == null){
            dirPath = "/" + dept.getId();
        } else {
            Department parentDept = departmentMapper.loadById(dept.getParent().getId());
            dirPath = parentDept.getDirPath() + "/" + dept.getId();
        }
        dept.setDirPath(dirPath);
        departmentMapper.update(dept);
    }

    /**
     * 获取部门树
     * @return
     */
    @Override
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }
}
